//
//  SkyHook.m
//  DNDzgz
//
//  Created by sid on 15/05/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SkyHook.h"
#include "IPAddress.h"

@implementation SkyHook

- (NSString *)getCurrentMAC
{
	InitAddresses();
	GetIPAddresses();
	GetHWAddresses();
	
	int i;
	NSString *deviceIP = nil;
	for (i=0; i<MAXADDRS; ++i)
	{
        static unsigned long localHost = 0x7F000001;            // 127.0.0.1
        unsigned long theAddr;
		
        theAddr = ip_addrs[i];
		
        if (theAddr == 0) break;
        if (theAddr == localHost) continue;
		
        NSLog(@"Name: %s MAC: %s IP: %s\n", if_names[i], hw_addrs[i], ip_names[i]);
		
        //decided what adapter you want details for
        if (strncmp(if_names[i], "en", 2) == 0)
        {
			deviceIP = [NSString stringWithFormat:@"%s", hw_addrs[i]];
			//NSLog(@"Adapter en has a IP of %@", [NSString stringWithFormat:@"%s", hw_addrs[i]]);
        }
	}
	
	return deviceIP;
}


- (NSString *)getCoordenatesIpodTouch
{
	
	//http://coderrr.wordpress.com/2008/09/10/get-the-physical-location-of-wireless-router-from-its-mac-address-bssid/
	
	NSString *urlString = [NSString stringWithFormat:@"http://api.skyhookwireless.com/wps2/location"];
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
	[request setURL:[NSURL URLWithString:urlString]];
	[request setHTTPMethod:@"POST"];
	
	//set headers
	NSString *contentType = [NSString stringWithFormat:@"text/xml"];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
	
	//create the body
	
	NSString *halfBody = @"<?xml version='1.0'?><LocationRQ xmlns='http://skyhookwireless.com/wps/2005' version='2.6' street-address-lookup='full'><authentication version='2.0'><simple><username>beta</username><realm>js.loki.com</realm></simple></authentication><access-point><mac>";
	NSString *fullBody = [NSString stringWithFormat:@"%@</mac><signal-strength>-50</signal-strength></access-point></LocationRQ>", [[self getCurrentMAC] stringByReplacingOccurrencesOfString:@":" withString:@""]];

	NSString *completeBody = [halfBody stringByAppendingString:fullBody];
	NSLog(@"%@", completeBody);
	
	NSMutableData *postBody = [NSMutableData data];
	//[postBody appendData:[[NSString stringWithFormat:@"<?xml version='1.0'?>"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[fullBody dataUsingEncoding:NSUTF8StringEncoding]];
	//[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
	
	//post
	[request setHTTPBody:postBody];
	
	//get response
	NSHTTPURLResponse* urlResponse = nil;  
	NSError *error = [[NSError alloc] init];  
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
	NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
	NSLog(@"Response Code: %d", [urlResponse statusCode]);
	if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
		NSLog(@"Response: %@", result);
		
		//here you get the response
		
	}

	return result;
}

@end
